﻿Public Class Customer

    Private Sub Customer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim databaseProvider As DatabaseProvider = databaseProvider.Instance()
        CustomerDataGrid.DataSource = databaseProvider.GetAllCustomers()
        CustomersToolStripMenuItem1.Enabled = False
    End Sub


    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub BtnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDel.Click
        Dim mResult
        Dim ObjConnection As New SqlClient.SqlConnection

        mResult = MsgBox("You are really want to delete the slected record?", vbYesNo + vbQuestion, "Removal Confirmation")

        If mResult = vbNo Then
            Exit Sub

        End If
        ObjConnection.ConnectionString = ("Database=bartek;Server=.\SQLEXPRESS;Integrated Security=True;")
        ObjConnection.Open()
        Try
            Dim ObjCommand As New SqlClient.SqlCommand
            ObjCommand.Connection = ObjConnection
            ObjCommand.CommandText = "delete from Customer_ID where ID='" & Me.CustomerDataGrid.CurrentRow.Cells(0).Value & "'"
            ObjCommand.ExecuteNonQuery()
        Finally
            ObjConnection.Close()
        End Try
        Me.CustomerDataGrid.Rows.Remove(Me.CustomerDataGrid.CurrentRow)

    End Sub
    Private Sub AdminToolsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Start.Show()
        Me.Close()
    End Sub

    Private Sub StaffMembersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StaffMembersToolStripMenuItem1.Click
        Employee.Show()
        Me.Close()
    End Sub

    Private Sub SalesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalesToolStripMenuItem1.Click
        Sales.Show()
        Me.Close()
    End Sub

    Private Sub OrdersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrdersToolStripMenuItem1.Click
        Orders.Show()
        Me.Close()
    End Sub

    Private Sub ProductsToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProductsToolStripMenuItem1.Click
        Product.Show()
        Me.Close()
    End Sub

    Private Sub ChooseLanguageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChooseLanguageToolStripMenuItem.Click
        Language.Show()
        Me.Close()
    End Sub

    Private Sub HelpToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpToolStripMenuItem1.Click
        Help.Show()
    End Sub

    Private Sub TxtName_TextChanged(sender As System.Object, e As System.EventArgs) Handles TxtName.TextChanged
        CustomerDataGrid.DataSource = customerSearch()
    End Sub

    Private Sub TxtSurname_TextChanged(sender As System.Object, e As System.EventArgs) Handles TxtSurname.TextChanged
        CustomerDataGrid.DataSource = customerSearch()
    End Sub

    Private Sub TxtTown_TextChanged(sender As System.Object, e As System.EventArgs) Handles TxtTown.TextChanged
        CustomerDataGrid.DataSource = customerSearch()
    End Sub

    Private Sub TxtPostcode_TextChanged(sender As System.Object, e As System.EventArgs) Handles TxtPostcode.TextChanged
        CustomerDataGrid.DataSource = customerSearch()
    End Sub

    Private Sub TxtCustID_TextChanged(sender As System.Object, e As System.EventArgs)
        CustomerDataGrid.DataSource = customerSearch()
    End Sub

    Private Function customerSearch() As BindingSource
        Dim databaseProvider As DatabaseProvider = databaseProvider.Instance()
        Return databaseProvider.FindCustomer(TxtName.Text, TxtSurname.Text, TxtTown.Text, TxtPostcode.Text)
    End Function

    Private Sub AdminToolsToolStripMenuItem_Click_1(sender As System.Object, e As System.EventArgs) Handles AdminToolsToolStripMenuItem.Click
        Start.Show()
        Me.Close()
    End Sub
End Class